import React from 'react';
import { fetchDepartment } from '../Department/service';
// import department from './department.json';

const useDepartmentList = params => {
    const [data, setData] = React.useState([]);
    const [total, setTotal] = React.useState(0);
    const [loading, setLoading] = React.useState(true);

    const fetchData = async () => {
        setLoading(true);
        const payload = {
            companyId: params.companyId || '',
            reportType: (params.dateType && params.dateType === 'month' ? 2 : 1) || '',
            startDate: params.startTime || '',
            endDate: params.endTime || ''
        };
        const response = await fetchDepartment(payload);
        if (response && response.code === 10000) {
            setData(response.data);
            setTotal(response.total);
        }
        setLoading(false);
    };

    React.useEffect(() => {
        fetchData();
    }, [params]);

    return [loading, data, total];
};

export default useDepartmentList;
